package com.bmf.module.examination.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.constant.AdminResultEnum;
import com.bmf.module.examination.entity.BigQuestion;
import com.bmf.module.examination.entity.FixedTestQuestion;
import com.bmf.module.examination.mapper.BigQuestionMapper;
import com.bmf.module.examination.service.BigQuestionService;
import com.bmf.module.examination.service.FixedTestQuestionService;
import com.bmf.module.examination.service.SelectionStrategyService;
import com.bmf.module.examination.vo.BigQuestionCreateVo;
import com.bmf.module.examination.vo.BigQuestionResultVo;
import com.bmf.module.examination.vo.BigQuestionUpdateVo;
import com.bmf.module.examination.vo.SelectionStrategyResultVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (BigQuestion)表服务实现类
 *
 * @author makejava
 * @since 2022-11-29 17:51:51
 */
@Service("bigQuestionService")
public class BigQuestionServiceImpl extends ServiceImpl<BigQuestionMapper, BigQuestion> implements BigQuestionService {

    @Autowired
    private SelectionStrategyService selectionStrategyService;

    @Autowired
    private FixedTestQuestionService fixedTestQuestionService;

    @Override
    public Integer insert(BigQuestionCreateVo bigQuestionCreateVo) {
        BigQuestion bigQuestion = BeanCopyUtils.copyBean(bigQuestionCreateVo, BigQuestion.class);
        save(bigQuestion);
        return bigQuestion.getId();
    }

    @Override
    public void update(BigQuestionUpdateVo bigQuestionUpdateVo) {
        BigQuestion bigQuestion = BeanCopyUtils.copyBean(bigQuestionUpdateVo, BigQuestion.class);
        boolean b = updateById(bigQuestion);
        AdminResultEnum.UPDATE_EXCEPTION.assertIsTrue(b);
    }

    @Override
    public List<BigQuestionResultVo> selectAll(Integer examinationId) {
        LambdaQueryWrapper<BigQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BigQuestion::getExaminationId, examinationId);
        List<BigQuestion> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<BigQuestionResultVo> list1 = BeanCopyUtils.copyBeanList(list, BigQuestionResultVo.class);
        List<Integer> ids = list1.stream().map(s -> s.getId()).collect(Collectors.toList());
        Map<Integer, List<SelectionStrategyResultVo>> selectionStrategyList = selectionStrategyService.getSelectionStrategyList(ids);
        return list1.stream().peek(s->{
            if(selectionStrategyList!=null){
                s.setSelectionStrategyResultVos(selectionStrategyList.get(s.getId()));
            }
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        removeByIds(ids);
        LambdaQueryWrapper<FixedTestQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FixedTestQuestion::getBigQuestionId,ids );
        fixedTestQuestionService.remove(wrapper);
    }

    @Override
    public void deleteByExaminationIds(List<Integer> ids) {
        LambdaQueryWrapper<BigQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(BigQuestion::getExaminationId,ids);
        remove(wrapper);
    }
}
